// pages/accountLogin/accountLogin.js
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
  },

  formSubmit(e){
    let {account,password} = e.detail.value

    wx.cloud.callFunction({
      name:'getUserData',
      data:{
        account:account
      }
    }).then(value=>{
      //flag为true，登录成功，跳转至个人中心
      let flag = this.check(value.result.data,password)

      if(flag){
        wx.setStorageSync('userInfo',value.result.data[0])
        app.golbalData.isLogin = true
        wx.reLaunch({
          url:'/pages/mine/mine'
        })
      }
    })
  },

  //验证账号是否存在，密码是否正确
  check(user,inputedPassword){
    let length = user.length
    let userInfo = user[0]
    if(length != 1){
      wx.showToast({
        title: '账号不存在',
        icon:'error',
        mask:true,
      })
      return false
    }else{
      let {password} = userInfo
      console.log(password)
      if(password != inputedPassword){
        wx.showToast({
          title: '密码错误',
          icon:'error',
          mask:true,
        })
        return false
      }
    }

    return true
  }
})